#!/bin/bash
set -e # 启用“错误立即退出”模式，当脚本中出现任何错误时，脚本立即退出

create_dir(){
    if [ ! -d "$1" ];then
        mkdir -p $1
        echo creating $1 ...
    else
        echo directory $1 already exist...
    fi
}

curr_root=$(cd $(dirname "$0");pwd) # 获取当前脚本
echo Current directory: $curr_root
# 如果命令行为clean，则清除编译路径
if [ $# -eq 1 ] && [ "${1}" = "clean" ];then
    if [ -d "$curr_root/build" ];then
        rm -rf $curr_root/build
    fi
    exit 0
fi
echo "***********Building work begin***********"
# 源文件目录
src_root=$curr_root/src
echo Source directory is: $src_root
build_root=$curr_root/build
echo Build directory is: $build_root
create_dir $build_root
cd $build_root
# 编译
cmake $src_root -DCMAKE_BUILD_TYPE=Release
make clean
make -j$(nproc)
echo "***********Building work finished***********"
